﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.Admin.Models;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class VendorController : Controller
    {
        IRestaurantVendorService _restaurantVendorService;
        IVendorIngredientService _vendorIngredientService;
        IVendorService _vendorService;
        public VendorController()
        {
            _restaurantVendorService = new RestaurantVendorService();
            _vendorService = new VendorService();
            _vendorIngredientService = new VendorIngredientService();
        }

        // GET: RRShop/Vendor
        public ActionResult Index(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        [Authorize]
        public ActionResult List(int id)
        {
            ViewBag.Id = id;

            var restaurantVendors = _restaurantVendorService.FindList(10, id, 1);
            List<RestaurantVendorViewModel> rvvms = new List<RestaurantVendorViewModel>();

            foreach (var rv in restaurantVendors)
            {
                rvvms.Add(RestaurantVendorViewModel.FromRestaurantVendor(rv));
            }

            return View(rvvms);
        }

        [Authorize]
        public ActionResult Bind(int id)
        {
            var vendorRestaurant = _restaurantVendorService.Find(id);
            ViewBag.Id = vendorRestaurant.Restaurant_Id;
            vendorRestaurant.Restaurant_Status = 1;
            vendorRestaurant.Vendor_Status = 1;

            _restaurantVendorService.Update(vendorRestaurant);

            return RedirectToAction("List", new { Id = vendorRestaurant.Restaurant_Id });

        }

        [Authorize]
        public ActionResult Details(int id, int rid)
        {
            ViewBag.Id = rid;
            var vendor = _vendorService.Find(id);
            return View(Yunwei.Web.Areas.Admin.Models.VendorViewModel.FromVendor(vendor));
        }

        [Authorize]
        public ActionResult Delete(int id, int rid)
        {
            var vendorRestaurant = _restaurantVendorService.Find(id);
            ViewBag.Id = vendorRestaurant.Restaurant_Id;
            _restaurantVendorService.Delete(vendorRestaurant);

            return RedirectToAction("List", new { Id = vendorRestaurant.Restaurant_Id });
        }

        [Authorize]
        public ActionResult Ingredients(int id, int vid, int rid)
        {
            ViewBag.Id = rid;

            var vis = _vendorIngredientService.FindList(100, "", vid, 0);
            List<VendorIngredientViewModel> vivms = new List<VendorIngredientViewModel>();
            foreach (var vi in vis)
            {
                VendorIngredientViewModel viv = VendorIngredientViewModel.FromVendorIngredient(vi);
                vivms.Add(viv);
            }

            return View(vivms);
        }
    }
}